//
//  BaseTableViewController.m
//  WisdomZhuHai
//
//  Created by yewei on 13-9-11.
//  Copyright (c) 2013年 wyd. All rights reserved.
//

#import "BaseTableViewController.h"
#import "TransforController.h"

@interface BaseTableViewController ()

@end

@implementation BaseTableViewController

@synthesize pullTableViewDelegate;
@synthesize tableStyle;
@synthesize frame;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
        tableStyle = style;
    }
    return self;
}

-(id)initWithFrame:(CGRect)f style:(UITableViewStyle)style
{
    self.frame = f;
    self = [super initWithStyle:style];
    if(self)
    {
        tableStyle = style;
    }
    return self;
}

- (id)initWithFrame:(CGRect)f style:(UITableViewStyle)style navigationBarHeight:(CGFloat)height {
    self.frame = f;
    self = [super initWithStyle:style];
    if(self)
    {
        tableStyle = style;
    }
    UIView *view_navigationBar = [TransforController shardeInstanceOfNavigationBarCoverViewWithY:height];
    [self.view.superview addSubview:view_navigationBar];
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
    if([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
    {
        [self setEdgesForExtendedLayout:UIRectEdgeLeft | UIRectEdgeRight];
    }
#endif
    NSString* backName = @"";
    if ([Common getIosVersion] < 7.0) {
        backName = NSLocalizedString(@"Back", nil);
    }
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:backName style:UIBarButtonItemStyleBordered target:nil action:nil];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return  UIInterfaceOrientationIsPortrait( interfaceOrientation );
}

// For ios6, use supportedInterfaceOrientations & shouldAutorotate instead
- (NSUInteger) supportedInterfaceOrientations{
#ifdef __IPHONE_6_0
    // return UIInterfaceOrientationMaskAllButUpsideDown;
    return UIInterfaceOrientationMaskPortrait;
#endif
}

- (BOOL) shouldAutorotate {
    return NO;
}

- (void)loadView {
    self.tableView = [[[PullTableView alloc] initWithFrame:self.frame style:tableStyle] autorelease];
    ((PullTableView *)(self.tableView)).pullBackgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.view = self.tableView;
    self.tableView.backgroundColor = [Common colorWithRGB:0xdbdcdc];
    [self setEnableRefresh:NO];
    [self setEnableLoadMore:NO];
}

- (void)setPullTableViewDelegate:(id<PullTableViewDelegate>)pullDelegate
{
    pullTableViewDelegate = pullDelegate;
    ((PullTableView*)self.tableView).pullDelegate = pullDelegate;
}

- (void)setEnableRefresh:(BOOL)flag
{
    ((PullTableView*)self.tableView).enableRefresh = flag;
}

- (void)setEnableLoadMore:(BOOL)flag
{
    ((PullTableView*)self.tableView).enableLoadingMore = flag;
}

- (void)setPullRefreshDate:(NSDate *)date
{
    ((PullTableView*)self.tableView).pullLastRefreshDate = date;
}

- (void)setPullTableIsRefreshing:(BOOL)refreshing
{
    ((PullTableView*)self.tableView).pullTableIsRefreshing = refreshing;
}
- (void)setPullTableIsLoadMore:(BOOL)loadMore
{
    ((PullTableView*)self.tableView).pullTableIsLoadingMore = loadMore;
}

- (void)reloadRowIndexInSection:(int)section row:(int)row
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
    NSArray *indexPaths = [[NSArray alloc] initWithObjects:indexPath, nil];
    [self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
    [indexPaths release];
}

@end
